/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import util.CommonUtil;

public class DbNewWord {

    private String wordGaul;
    private String wordInd;
    private static final String RMS_NAME = "kamus_newword"; //nama RMS
    private static final String SEPARATOR = "\n";
    private static final String SEPARATOR_WORD = "~";
    private int length = 0;

    public DbNewWord() {
    }

    /**
     * ambil data dari RMS
     */
    public void load() {
        String result = Http.readFromRMS(RMS_NAME);
        if (result.length() == 0) { //init pertama kali
            clear();
            return;
        }
        String[] arrResult = CommonUtil.splitStr(result, SEPARATOR);
        setWordGaul(arrResult[0]);
        setWordInd(arrResult[1]);
        setLength(CommonUtil.splitStr(arrResult[0], SEPARATOR_WORD).length);
    }

    /**
     * simpan data ke RMS
     * @param wordGaul  kata gaul dipisahkan dengan tanda ~
     * @param wordInd   kata ind dipisahkan dengan tanda ~
     */
    public void save(String wordGaul, String wordInd) {
        StringBuffer data = new StringBuffer();
        data.append(wordGaul).append(SEPARATOR).append(wordInd);
        Http.saveToRMS(RMS_NAME, data.toString().getBytes(), true);
        setWordGaul(wordGaul);
        setWordInd(wordInd);
    }

    /**
     * membersihkan RMS
     */
    public void clear() {
        setWordGaul("");
        setWordInd("");
        Http.saveToRMS(RMS_NAME, "".getBytes(), true);
        setLength(0);
    }

    /**
     * menambah pasangan data gaul-ind
     * @param wordGaul kata gaul
     * @param wordInd kata ind
     */
    public void add(String wordGaul, String wordInd) {
        StringBuffer resultGaul = new StringBuffer(getWordGaul());
        StringBuffer resultInd = new StringBuffer(getWordInd());
        if (getLength() == 0) {
            resultGaul.append(wordGaul);
            resultInd.append(wordInd);
        } else {
            resultGaul.append(SEPARATOR_WORD).append(wordGaul);
            resultInd.append(SEPARATOR_WORD).append(wordInd);
        }
        System.out.println("RESULT GAUL="+resultGaul.toString());
        System.out.println("RESULT IND="+resultInd.toString());
        save(resultGaul.toString(), resultInd.toString());
        setWordGaul(resultGaul.toString());
        setWordInd(resultInd.toString());
        setLength(getLength() + 1);
    }

    /**
     * mengambil pasangan kata gaul-ind
     * @return
     */
    public String[] getPairWord() {
        String[] result=null;
        if (getLength() == 0) {
            result = new String[0];
        } else {
            String[] arrWordGaul = CommonUtil.splitStr(getWordGaul(), SEPARATOR_WORD);
            String[] arrWordInd = CommonUtil.splitStr(getWordInd(), SEPARATOR_WORD);
            result = new String[getLength()];
            for (int i = 0; i < getLength(); i++) {
                System.out.println(i + ":" + arrWordGaul[i] + "-" + arrWordInd);
                result[i] = arrWordGaul[i] + "=" + arrWordInd[i];
            }
        }
        return result;
    }

    public String getWordGaul() {
        return wordGaul;
    }

    public void setWordGaul(String wordGaul) {
        this.wordGaul = wordGaul;
    }

    public String getWordInd() {
        return wordInd;
    }

    public void setWordInd(String wordInd) {
        this.wordInd = wordInd;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
